package com.plm.maque.controller;

import com.alibaba.fastjson.JSON;
import com.plm.maque.dao.UserDao;
import com.plm.maque.entity.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * mongodb 用户控制层代码示例
 * @author Limi
 * @create 2018-12-30 21:49
 */
@RestController
@RequestMapping("/m_user")
public class MongoUserController {

    @Autowired
    private UserDao userDao;

    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public void saveUser(@RequestParam Map<String, Object> params){
        User user = JSON.parseObject(JSON.toJSONString(params), User.class);
        userDao.saveUser(user);
    }

    @RequestMapping(value = "/get", method = RequestMethod.GET)
    @ResponseBody
    public User queryUserByUsername(@RequestParam(name="username") String username){
        User user = userDao.findByUsername(username);
        return user;
    }

    @RequestMapping(value = "/{userId}", method = RequestMethod.GET)
    @ResponseBody
    public User queryUserByUserId(@PathVariable("userId") Long userId){
        User user = userDao.findByUserId(userId);
        return user;
    }

    @RequestMapping(value = "/update", method = RequestMethod.PATCH)
    public void updateUser(@RequestParam Map<String, Object> params){
        User user = JSON.parseObject(JSON.toJSONString(params), User.class);
        userDao.updateUser(user);
    }

    @RequestMapping(value = "/{userId}", method = RequestMethod.DELETE)
    public void deleteUser(@PathVariable("userId") Long userId){
        userDao.deleteByUserId(userId);
    }
}
